<?php

namespace services\member;

use common\components\Service;
use common\models\member\Member;
use common\models\member\MemberProject;
use Yii;

/**
 * Class MemberProjectService
 * @package services\member
 *
 */
class MemberProjectService extends Service
{
    /**
     * 根据项目ID查找用户IDs
     * @param $projectIds
     * @return int[]
     */
    public static function getProjectIdsByMemberId($memberIds): array
    {
        return MemberProject::find()
            ->select(['project_id'])
            ->where(['member_id' => $memberIds])
            ->column();
    }

    /**
     * 根据项目ID查找用户IDs
     * @param $projectIds
     * @return int[]
     */
    public static function getMemberIdsByProjectId($projectIds): array
    {
        return MemberProject::find()
            ->select(['member_id'])
            ->where(['project_id' => $projectIds])
            ->column();
    }

    /**
     * 根据项目获取项目管理员人员IDs
     * @param $projectIds
     * @return int[]
     */
    public static function getAdminMemberIdsByProjectId($projectIds): array
    {
        return MemberProject::find()
            ->select(['member_id'])
            ->where([
                'project_id' => $projectIds,
                'leader_type' => MemberProject::LEADER_TYPE_ADMIN
            ])
            ->column();
    }

    /**
     * 根据用户获取项目管理员项目IDs
     * @param $memberIds
     * @return int[]
     */
    public static function getAdminProjectIdsByMemberId($memberIds): array
    {
        return MemberProject::find()
            ->select(['project_id'])
            ->where([
                'member_id' => $memberIds,
                'leader_type' => MemberProject::LEADER_TYPE_ADMIN
            ])
            ->column();
    }

    /**
     * 编辑项目
     * @param Member $member
     * @param array $projectIds
     * @return void
     * @throws \Exception
     */
    public static function editModel(Member $member, array $projectIds): void
    {
        // 原项目IDs
        $oldProjectIds = self::getProjectIdsByMemberId($member->id);
        if ($delProjectIds = array_diff($oldProjectIds, $projectIds)) {
            MemberProject::deleteAll([
                'member_id' => $member->id,
                'project_id' => $delProjectIds,
            ]);
        }
        if ($addProjectIds = array_diff($projectIds, $oldProjectIds)) {
            $data = [];
            foreach ($addProjectIds as $id) {
                $data[] = [$member->id, $id];
            }
            Yii::$app->db->createCommand()->batchInsert(
                MemberProject::tableName(),
                ['member_id', 'project_id'],
                $data
            )->execute();
        }
    }

    /**
     * 解绑项目
     * @param int $projectId
     * @return void
     */
    public static function unBindProject(int $projectId): void
    {
        MemberProject::deleteAll(['project_id' => $projectId]);
    }

    /**
     * 设置项目IDs
     * @param int $projectId
     * @param int|array $memberIds
     * @return void
     */
    public static function editProjectAdmin(int $projectId, $memberIds): void
    {
        // 清空项目管理员
        MemberProject::updateAll(['leader_type' => MemberProject::LEADER_TYPE_NORMAL], ['project_id' => $projectId]);
        // 设置项目管理员
        if ($memberIds) {
            MemberProject::updateAll(['leader_type' => MemberProject::LEADER_TYPE_ADMIN], [
                'project_id' => $projectId,
                'member_id' => $memberIds,
            ]);
        }
    }

    /**
     * 根据项目统计人员数
     * @param $projectIds
     * @return array
     */
    public static function getStaticsNumByProject($projectIds): array
    {
        return MemberProject::find()->alias('mp')
            ->select(['mp.project_id', 'COUNT(*) member_num'])
            ->innerJoin(Member::tableName().' m', "m.id = mp.member_id")
            ->where([
                'mp.project_id' => $projectIds,
            ])
            ->andWhere(['!=', 'm.status', Member::STATUS_DELETE])
            ->indexBy('project_id')
            ->groupBy('mp.project_id')
            ->asArray()
            ->all();
    }
}
